package com.baiqian.doupo.keyboard;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.baiqian.doupo.gui.PlayerRattributeGui;
import com.baiqian.doupo.gui.PlayerSkillGui;
import com.baiqian.doupo.network.Networking;
import com.baiqian.doupo.network.SendPack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class KeyBoardInput {
    //修炼
    public static final KeyBinding MESSAGE_KEY_G = new KeyBinding("key.message_G",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.SHIFT,//SHIFT
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_G,//G键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //提纯
    public static final KeyBinding MESSAGE_KEY_H = new KeyBinding("key.message_H",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.SHIFT,//SHIFT
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_H,//H键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //C技能
    public static final KeyBinding MESSAGE_KEY_C = new KeyBinding("key.message_C",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_C,//C键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //V技能
    public static final KeyBinding MESSAGE_KEY_V = new KeyBinding("key.message_V",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_V,//V键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //B技能
    public static final KeyBinding MESSAGE_KEY_B = new KeyBinding("key.message_B",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_B,//B键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //N技能
    public static final KeyBinding MESSAGE_KEY_N = new KeyBinding("key.message_N",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_N,//N键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //Y配置技能
    public static final KeyBinding MESSAGE_KEY_Y = new KeyBinding("key.message_Y",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_Y,//Y键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //U个人面板
    public static final KeyBinding MESSAGE_KEY_U = new KeyBinding("key.message_U",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_U,//U键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //↑增大斗气输出度
    public static final KeyBinding MESSAGE_KEY_UP = new KeyBinding("key.message_UP",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_UP,//UP键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //↓减小斗气输出度
    public static final KeyBinding MESSAGE_KEY_DOWN = new KeyBinding("key.message_DOWN",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_DOWN,//DOWN键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //Shfit+空格
    public static final KeyBinding MESSAGE_KEY_SPACE = new KeyBinding("key.message_SPACE",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.SHIFT,//SHIFT
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_SPACE,//空格
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //←斗技组-1
    public static final KeyBinding MESSAGE_KEY_LEFT = new KeyBinding("key.message_LEFT",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_LEFT,//LEFT键
            "key.category." + Utils.MOD_ID//按键列表名
    );
    //→斗技组+1
    public static final KeyBinding MESSAGE_KEY_RIGHT = new KeyBinding("key.message_RIGHT",
            KeyConflictContext.IN_GAME,//游戏内按下
            KeyModifier.NONE,//NONE
            InputMappings.Type.KEYSYM,//键盘
            GLFW.GLFW_KEY_RIGHT,//RIGHT键
            "key.category." + Utils.MOD_ID//按键列表名
    );

    private static boolean is_flag_g = false;
    private static boolean is_flag_h = false;

    @SubscribeEvent//监听键盘输入事件
    public static void onKeyboardInput(InputEvent.KeyInputEvent event) {
        PlayerEntity player = Minecraft.getInstance().player;
        LazyOptional<IDouQiCapability> douQiCap;
        try {
            douQiCap = player.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
        } catch (NullPointerException e) {
            return;
        }
        if (MESSAGE_KEY_G.isPressed()) {
            douQiCap.ifPresent((l) -> {
                        if (is_flag_g) {
                            if (l.getLevelBig() > 0 || player.experienceLevel >= 30) {
                                if (l.isToLevelSmall() || l.isToLevelMiddle() || l.isToLevelBig()) {
                                    is_flag_g = false;
                                    //表示我松开了G
                                    Networking.INSTANCE.sendToServer(new SendPack( 4,player.getUniqueID(),'G'));
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".to_level"), player.getUniqueID());
                                }
                                else {
                                    //表示我按下了G
                                    //l.addDouQi();
                                    Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'G'));
                                }
                            }
                        } else if (!(l.isToLevelSmall() || l.isToLevelMiddle() || l.isToLevelBig())) {
                            is_flag_g = true;
                            if (l.getLevelBig() == 0 && player.experienceLevel < 30) {
                                if (player.experienceLevel < 1)
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_1"), player.getUniqueID());
                                else if (player.experienceLevel < 5)
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_2"), player.getUniqueID());
                                else if (player.experienceLevel < 10)
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_3"), player.getUniqueID());
                                else if (player.experienceLevel < 20)
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_4"), player.getUniqueID());
                                else if (player.experienceLevel < 29)
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_5"), player.getUniqueID());
                                else
                                    player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G_6"), player.getUniqueID());
                            }
                            else {
                                player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_G"), player.getUniqueID());
                            }
                        }
                    }
            );
        }
        else if (MESSAGE_KEY_H.isPressed()) {
            douQiCap.ifPresent((l) -> {
                if (is_flag_h) {
                    if (l.getLevelBig() > 0) {
                        if (l.isToLevelSmall() || l.isToLevelMiddle() || l.isToLevelBig()) {
                            is_flag_h = false;
                            Networking.INSTANCE.sendToServer(new SendPack(4,player.getUniqueID(),'H'));
                            player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".to_level"), player.getUniqueID());
                        } else {
                            Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'H'));
                        }
                    }
                } else if (!(l.isToLevelSmall() || l.isToLevelMiddle() || l.isToLevelBig())) {
                    is_flag_h = true;
                    if (l.getLevelBig() == 0) {
                        player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_H_not"), player.getUniqueID());
                    } else {
                        player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".press_H"), player.getUniqueID());
                    }
                }
            });
        }
        else if (is_flag_g && MESSAGE_KEY_G.getKey().getKeyCode() == event.getKey()) {
            is_flag_g = false;
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if (l.getLevelBig() > 0 || player.experienceLevel >= 30) {
                        player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".unpress_G"), player.getUniqueID());
                    }
                    Networking.INSTANCE.sendToServer(new SendPack(4,player.getUniqueID(),'G'));
                });
            }
        }
        else if (is_flag_h && MESSAGE_KEY_H.getKey().getKeyCode() == event.getKey()) {
            is_flag_h = false;
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if (l.getLevelBig() > 0) {
                        player.sendMessage(new TranslationTextComponent("message." + Utils.MOD_ID + ".unpress_H"), player.getUniqueID());
                        Networking.INSTANCE.sendToServer(new SendPack(4,player.getUniqueID(),'H'));
                    }
                });
            }
        }
        else if (MESSAGE_KEY_C.isKeyDown()) {
            Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'C'));
        } else if (MESSAGE_KEY_V.isKeyDown()) {
            Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'V'));
        } else if (MESSAGE_KEY_B.isKeyDown()) {
            Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'B'));
        } else if (MESSAGE_KEY_N.isKeyDown()) {
            Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'N'));
        } else if (MESSAGE_KEY_Y.isKeyDown()) {
            Minecraft.getInstance().displayGuiScreen(new PlayerSkillGui(new TranslationTextComponent(Utils.MOD_ID + ".player_skill_gui")));
        } else if (MESSAGE_KEY_U.isKeyDown()) {
            Minecraft.getInstance().displayGuiScreen(new PlayerRattributeGui(new TranslationTextComponent(Utils.MOD_ID + ".player_rattribute_gui")));
        }
        else if (MESSAGE_KEY_UP.isKeyDown()) {
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if (l.getLevelBig() > 0) {
                        Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'↑'));
                    }
                });
            }
        }
        else if (MESSAGE_KEY_DOWN.isKeyDown()) {
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if (l.getLevelBig() > 0) {
                        Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),'↓'));
                    }
                });
            }
        }
        else if(MESSAGE_KEY_SPACE.isKeyDown()){
            douQiCap.ifPresent((l) -> {
                if(l.getLevelBig()>=6){
                    Networking.INSTANCE.sendToServer(new SendPack(3,player.getUniqueID(),' '));
                }
            });
        }
        else if (MESSAGE_KEY_LEFT.isKeyDown()) {
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if(l.getDouJiGroupChoose()>0) {
                        Networking.INSTANCE.sendToServer(new SendPack(8,"left"));
                    }
                });
            }
        }
        else if (MESSAGE_KEY_RIGHT.isKeyDown()) {
            if (douQiCap.isPresent()) {
                douQiCap.ifPresent((l) -> {
                    if(l.getDouJiGroupChoose()<3) {
                        Networking.INSTANCE.sendToServer(new SendPack(8,"right"));
                    }
                });
            }
        }
    }
}

//注册快捷键
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
class KeybindingRegistry {
    @SubscribeEvent
    public static void onClientSetup(FMLClientSetupEvent event) {
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_G));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_H));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_C));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_V));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_B));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_N));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_Y));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_U));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_UP));
        event.enqueueWork(() -> ClientRegistry.registerKeyBinding(KeyBoardInput.MESSAGE_KEY_DOWN));
    }
}
